﻿using System;

namespace FogBugzPd.Web.Utils
{
	public static class StringUtils
	{
		public static string FormatDateTime(this DateTime? dateTime)
		{
			if (!dateTime.HasValue) return string.Empty;
			return dateTime.Value.FormatDateTime();
		}

		public static string FormatTime(DateTime? dateTime)
		{
			if (dateTime == null)
				return "";

			string result = dateTime.Value.ToString("h\\:mmtt");
			return result.ToLower();
		}

		public static string FormatDateTime(this DateTime dateTime)
		{
			return FormatShortDate(dateTime) + "  " + FormatTime(dateTime);
		}
		
		public static string FormatShortDate(this DateTime? dateTime)
		{
			if (!dateTime.HasValue) return string.Empty;
			return dateTime.Value.FormatShortDate();
		}

		public static string FormatShortDate(this DateTime dateTime)
		{
			return dateTime.ToString("MMM dd, yyyy");
		}

		public static string FormatDuration(this int durationHours)
		{
			return Convert.ToDecimal(durationHours).FormatDuration();
		}

		public static string FormatDurationMin(this int durationMins)
		{
			return (Convert.ToDecimal(durationMins)/60).FormatDuration();
		}

		public static string FormatDuration(this decimal hours)
		{
			if (hours == 0) return "0 hours";

			if (hours < 8)
			{
				return string.Format("{0:0.#} hours", Math.Round(hours, 1, MidpointRounding.AwayFromZero));
			}

			return string.Format("{0:0.#} days", Math.Round(hours/8, 1, MidpointRounding.AwayFromZero));
		}
	}
}
